clear all
*cap log close
set more off

// Directories ------------
*global PROJ   = "/Users/f00451k/Dropbox (Dartmouth College)/projects/deterrence/replication"
*global data   = "${PROJ}/data/out"
*global temp   = "${PROJ}/_temp"
// -------------------------



// construct data with offense counts as outcomes ---------------------------
use "${data}/out/4-main", clear
keep citationid dlnumber offensedate 
joinby dlnumber using "${data}/out/1-offending"

gen n_cite1  = (citation==1) & (date-offensedate>0) & (date-offensedate<=365)
gen n_speed1 = (viol==575)   & (date-offensedate>0) & (date-offensedate<=365)
gen n_crash1 = (crash==1)	 & (date-offensedate>0) & (date-offensedate<=365)
gen n_cite2  = (citation==1) & (date-offensedate>0) & (date-offensedate<=730)
gen n_speed2 = (viol==575)   & (date-offensedate>0) & (date-offensedate<=730)
gen n_crash2 = (crash==1)    & (date-offensedate>0) & (date-offensedate<=730)

collapse (sum) n_*, by(citationid)
tempfile tempdata
save    `tempdata'


use "${data}/out/4-main", clear
*drop cite_py1-crashinj_ny99
merge 1:1 citationid using `tempdata', keep(1 3) nogen

foreach y of varlist n_* {
	replace `y' = 0  if mi(`y')
	replace `y' = 12 if `y' > 12
}

drop crash_ny1 
rename crashany_ny1 crash_ny1 
// clean up (store trimmed dataset) --------------
#delimit ;
keep year citationid officerid harsh Z covbin1 covbin2 totfe lenient*
cite_ny1 speed_ny1 crash_ny1 n_cite2 n_speed2 n_crash2 contest  ;
order year citationid officerid harsh Z covbin1 covbin2 totfe lenient*
cite_ny1 speed_ny1 crash_ny1 n_cite2 n_speed2 n_crash2 contest  ;
#delimit cr 

qui compress
save "${temp}/data_extrap", replace 
// ------------------------------------------------------------------------



// construct data with reoffending speeds as outcomes ---------------------
use "${data}/out/4-main", clear
keep citationid dlnumber offensedate 

joinby dlnumber using "${data}/out/1-offending"
gen ny1  = (date - offensedate>1) & (date - offensedate<=365)

foreach sp in 5 10 15 20 25 30 {
	gen speed_ny1_`sp' = (ny1==1) & (citation==1) & (viol==575) & (speeddiff>=`sp') & !mi(speeddiff)
}
collapse (max) speed_ny1_*, by(citationid) 
qui compress
save "${temp}/temp_speeds", replace 


use "${data}/out/4-main", clear
keep year citationid officerid harsh Z covbin1 covbin2 totfe lenient*
merge 1:1 citationid using "${temp}/temp_speeds", keep(1 3) nogen 

foreach sp in 5 10 15 20 25 30 {
	rename speed_ny1_`sp' speed_`sp'
	replace speed_`sp'=0 if mi(speed_`sp')
}

save "${temp}/speeds_data", replace 
rm   "${temp}/temp_speeds.dta"
// ------------------------------------------------------------------------





